package com.gavin.book.service;

import com.gavin.book.enums.BookStatusEnum;
import com.gavin.book.mapper.BookMapper;
import com.gavin.book.model.BookInfo;
import com.gavin.book.model.PageRequest;
import com.gavin.book.model.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {

    @Autowired
    private BookMapper bookMapper;

    public void addBook(BookInfo bookInfo) {
        bookMapper.addBook(bookInfo);
    }

    public ResponseResult<BookInfo> getListByPage(PageRequest pageRequest){
        //1.获得总图书数
        //2.获得当前页数据
        Integer count=bookMapper.count();

        List<BookInfo> bookInfos=bookMapper.selectBooksByPage(pageRequest);
        for (BookInfo bookInfo:bookInfos){
            bookInfo.setStatusCN(BookStatusEnum.getStatusByCode(bookInfo.getStatus()).getDesc());
        }
        return new ResponseResult<>(count, bookInfos, pageRequest);
    }

    public BookInfo querBookById(Integer bookId) {
        return bookMapper.queryBookById(bookId);
    }

    public void updateBook(BookInfo bookInfo) {
        bookMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List<Integer> ids){
        return bookMapper.bstchDelete(ids);

    }
}
